var 
    express = require('express'),
    router = express.Router(),
    uuid = require('uuid'),
    us = require('underscore'),
    iputil = require('ip'),
    mongos = require('../service/mongodb');





// router.get('/port/:port', function(req, res) {
//     var port = req.params.port;
//     mongos.find('ports',{'port':port},'address:desc')
//     .then(function(docs){
//         res.send(docs);  
//     });   
// });

router.get('/subnet', function(req, res){
    mongos.group('ports', 'network' , function(docs){
        res.send(us.sortBy(docs,function(e){
            return iputil.toLong(e.network);
        }));  
    });
});

router.get('/ip/:ip', function(req, res) {
    var ip = req.params.ip;
    mongos.get('ports',{'address':ip})
    .then(function(doc){
        res.send(doc);  
    });   
});

router.get('/sb/:sb', function(req, res) {
    var sb = req.params.sb;
    
    mongos.find('ports',{'network':sb},'address:desc')
    .then(function(docs){
        res.send(us.sortBy(docs,function(e){
            return iputil.toLong(e.address);
        }));  
    });   
});


module.exports = router;